package com.jf3q.sup.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.jf3q.sup.dto.SalesOrdersDto;
import com.jf3q.sup.service.CarriersService;
import com.jf3q.sup.service.CustomersService;
import com.jf3q.sup.service.ProductsService;
import com.jf3q.sup.service.SalesOrdersService;
import com.jf3q.sup.vo.ResultVo;
import com.jf3q.sup.vo.SaleShowIndexVo;
import com.jf3q.sup.vo.SalesOrdersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 销售订单controller
 */
@RestController
@RequestMapping("sales/orders")
public class SalesOrdersController {

    @Autowired
    private SalesOrdersService salesOrdersService;

    @Autowired
    private CustomersService customersService;

    @Autowired
    private ProductsService productsService;

    @Autowired
    private CarriersService carriersService;


    /**
     * 根据id查询销售订单信息
     */
    @GetMapping("{id}")
    public ResultVo getOrders(@PathVariable Integer id){
        SalesOrdersVo salesOrdersVo=null;
        try {
            salesOrdersVo=salesOrdersService.getOrders(id);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return ResultVo.success(null,salesOrdersVo);
    }

    /**
     * 分页查询销售订单信息
     */
    @PreAuthorize("hasAnyAuthority('/salesOrders')")
    @PostMapping("getPage")
    public ResultVo getPage(@RequestBody SalesOrdersDto salesOrdersDto,
                            @RequestParam(defaultValue = "1")Integer pageNum){
        IPage page=null;
        try {
            page=salesOrdersService.pages(salesOrdersDto,pageNum);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return ResultVo.success(null,page);
    }

    /**
     * 新增销售订单信息
     */
    @PreAuthorize("hasAnyAuthority('/salesOrders/add')")
    @PostMapping("")
    public ResultVo add(@RequestBody SalesOrdersDto salesOrdersDto){
        try {
            salesOrdersService.addOrUpdate(salesOrdersDto);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return ResultVo.success("新增成功！",null);
    }

    /**
     * 修改销售订单信息
     */
    @PreAuthorize("hasAnyAuthority('/salesOrders/edit')")
    @PutMapping("")
    public ResultVo update(@RequestBody SalesOrdersDto salesOrdersDto){
        try {
            salesOrdersService.addOrUpdate(salesOrdersDto);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return ResultVo.success("修改成功！",null);
    }

    /**
     * 删除销售订单信息
     */
    @PreAuthorize("hasAnyAuthority('/salesOrders/delete')")
    @DeleteMapping("{ids}")
    public ResultVo del(@PathVariable Integer[] ids){
        try {
            salesOrdersService.del(ids);
        } catch (Exception e) {
            throw new RuntimeException("删除失败！");
        }
        return ResultVo.success("删除成功！",null);
    }

    @PostMapping("/selSaleInMouth/{time}")
    public ResultVo selSaleInMouth(@PathVariable String time){
        System.out.println(time);
        Map map= null;
        try {
            List<String> name=salesOrdersService.getSaleShow(time).stream().map(SaleShowIndexVo::getName).toList();
            List<Integer> quantity=salesOrdersService.getSaleShow(time).stream().map(SaleShowIndexVo::getQuantity).toList();
            map = new HashMap();
            map.put("name",name);
            map.put("quantity",quantity);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultVo.success("",map);
    }


    /**
     * 获取所有客服
     */
    @GetMapping("getCustomers")
    public ResultVo getCustomers(){
        return ResultVo.success(null,customersService.list());
    }

    /**
     * 产品信息
     */
    @GetMapping("getProducts")
    public ResultVo getProducts(){
        return ResultVo.success(null,productsService.list());
    }

    /**
     * 承运商信息
     */
    @GetMapping("getCarriers")
    public ResultVo getCarriers(){
        return ResultVo.success(null,carriersService.list());
    }



}
